package animator.phantom.renderer.plugin.editlayer;

/*
    Copyright Janne Liljeblad 2006,2007,2008

    This file is part of Phantom2D.

    Phantom2D is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Phantom2D is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Phantom2D.  If not, see <http://www.gnu.org/licenses/>.
*/

import animator.phantom.gui.view.*;
import animator.phantom.gui.view.PressCaptureShape;
import animator.phantom.plugin.PluginEditLayer;
import animator.phantom.renderer.plugin.FileSource3DPlugin;
import java.awt.Graphics2D;
import java.awt.geom.Point2D;
import animator.phantom.renderer.param.AnimCoords3D;
import java.util.Vector;
import animator.phantom.gui.GUIResources;
import javax.swing.ImageIcon;

public class FileSource3DPluginEditLayer extends PluginEditLayer
{
	private int MOVE_3D_MODE = CUSTOM_EDIT_MODE_1;
	private int ROTATE_3D_MODE = CUSTOM_EDIT_MODE_2;
	private int SCALE_3D_MODE = CUSTOM_EDIT_MODE_3;
	private boolean moveIsXY = true;//is set false for right mouse xz move edit
	private boolean rotationIsXY = true;//is set false for right mouse xz rotation edit

	private float MOVIE_COODRS_TO_POS_MULT = 0.006f;// 100 pixels in movie space changes 3d position with 1.0
	private float MOVIE_COODRS_TO_ROT_MULT = 0.6f;// 100 pixels in movie space changes 3d rotation with 100 degrees

	private float startX = 0;
	private float startY = 0;
	private float startZ = 0;

	private float startXRot = 0;
	private float startYRot = 0;
	private float startZRot = 0;

	private FileSource3DPlugin plugin;

	public FileSource3DPluginEditLayer( FileSource3DPlugin p )
	{
		super( p );
		plugin = p;
		PressCaptureShape  editShape = new  PressCaptureShape( this );
		registerShape( editShape );

		Vector<Integer> buttons = new Vector<Integer>();
		buttons.add( new Integer( MOVE_3D_MODE ));
		buttons.add( new Integer( ROTATE_3D_MODE ));
		buttons.add( new Integer( SCALE_3D_MODE ));
		setButtonsData( buttons, 0 );

		ImageIcon move3D = GUIResources.getIcon( GUIResources.move3D );
		ImageIcon move3DPressed = GUIResources.getIcon( GUIResources.move3DPressed );
		setModeButtonIcons( MOVE_3D_MODE, move3D, move3DPressed );
 		setModeButtonIcons( ROTATE_3D_MODE, GUIResources.getIcon( GUIResources.rotate ), GUIResources.getIcon( GUIResources.rotatePressed ) );
	}

	public void frameChanged( int frame )
	{
		
	}

	public void modeChanged( int newMode )
	{

	}
	

	public void mousePressed( int frame, Point2D.Float startPoint )
	{
		AnimCoords3D coords = plugin.get3DCoords();
		if( getMode() == MOVE_3D_MODE )
		{
			if( lastPressWasLeftMouse() )
			{
				moveIsXY = true;
				startX = coords.x.getValue( frame );
				startY = coords.y.getValue( frame );
			}
			else
			{
				moveIsXY = false;
				startX = coords.x.getValue( frame );
				startZ = coords.y.getValue( frame );
			}
		}
		else if( getMode() == ROTATE_3D_MODE )
		{
			if( lastPressWasLeftMouse() )
			{
				rotationIsXY = true;
				startXRot = coords.rotation.getValue( frame );
				startYRot = coords.rotY.getValue( frame );
			}
			else
			{
				rotationIsXY = false;
				startZRot = coords.rotZ.getValue( frame );
			}
		}
		
	}

	public void mouseDragged( int frame, Point2D.Float startPoint, Point2D.Float dragPoint )
	{
		Point2D.Float delta = getMouseDelta();
		if( getMode() == MOVE_3D_MODE )
		{
			if( moveIsXY )
				updateXY( delta, frame );
			else
				updateZ( delta, frame );
		}
		else if( getMode() == ROTATE_3D_MODE )
		{
			if( rotationIsXY )
				updateXYRotation( delta, frame );
			else
				updateZRotation( delta, frame );
		}
	}

	public void mouseReleased( int frame, Point2D.Float startPoint, Point2D.Float relesePoint )
	{
		Point2D.Float delta = getMouseDelta();
		if( getMode() == MOVE_3D_MODE )
		{
			if( moveIsXY )
				updateXY( delta, frame );
			else
				updateZ( delta, frame );
			plugin.get3DCoords().x.registerUndo();
			plugin.get3DCoords().z.registerUndo( false );//to pack all as single undo;
			plugin.get3DCoords().y.registerUndo( false );//to pack all as single undo;
		}
		else if( getMode() == ROTATE_3D_MODE )
		{
			if( rotationIsXY )
				updateXYRotation( delta, frame );
			else
				updateZRotation( delta, frame );
			plugin.get3DCoords().rotation.registerUndo();
			plugin.get3DCoords().rotY.registerUndo( false );
			plugin.get3DCoords().rotZ.registerUndo( false );
		}
	}

	private void updateXY( Point2D.Float delta, int frame )
	{
		float newX = startX + delta.x * MOVIE_COODRS_TO_POS_MULT;
		float newY = startY + delta.y * MOVIE_COODRS_TO_POS_MULT;

		AnimCoords3D coords = plugin.get3DCoords();
		coords.x.setValue( frame, newX );
		coords.y.setValue( frame, newY );
	}

	private void updateZ( Point2D.Float delta, int frame )
	{
		float newZ = startZ + delta.y * MOVIE_COODRS_TO_POS_MULT;

		AnimCoords3D coords = plugin.get3DCoords();
		coords.z.setValue( frame, newZ );
	}

	private void updateXYRotation( Point2D.Float delta, int frame )
	{
		float newXRot = startXRot + delta.y * MOVIE_COODRS_TO_ROT_MULT;
		float newYRot = startYRot + delta.x * MOVIE_COODRS_TO_ROT_MULT;//coords switched to match user expectation

		AnimCoords3D coords = plugin.get3DCoords();
		coords.rotation.setValue( frame, newXRot );
		coords.rotY.setValue( frame, newYRot );
	}

	private void updateZRotation( Point2D.Float delta, int frame )
	{
		float newZRot = startZRot + delta.x * MOVIE_COODRS_TO_ROT_MULT;

		AnimCoords3D coords = plugin.get3DCoords();
		coords.rotZ.setValue( frame, newZRot );
	}

	public void paintLayer( Graphics2D g )
	{

	}

}//end class

